<?php
namespace lib;
class Mysql {
    
    private $client = null;
    
    private $querySql = '';
    
    public $lastSql = '';
    
    public function __construct() {
        $dbConfig = include __DIR__ . '/../../config/database.php';
        $this->client = new \Swoole\Coroutine\MySQL();
        $this->client->connect(['database' => $dbConfig['database'], 'host' => $dbConfig['hostname'], 'port' => $dbConfig['hostport'], 'user' => $dbConfig['username'], 'password' => $dbConfig['password']]);
    }
    
    public function query($sql) {
        return $this->client->query($sql);
    }
    
    public function queryOne($sql) {
        $res = $this->client->query($sql);
        if(empty($res)){
            return [];
        }else{
            return $res[0];
        }
    }
    
    public function addAll($table, $data) {
        $fields = array_keys($data[0]);
        foreach ($fields as &$field) {
            $field = '`' . $field . '`';
        }
        $fields = implode(',', $fields);
        $values = [];
        foreach ($data as $item) {
            $vs = [];
            foreach ($item as $v) {
                array_push($vs, '"' . $v . '"');
            }
            
            array_push($values, '(' . implode(',', $vs) . ')');
        }
        
        $values = implode(',', $values);
        
        $sql = "insert into `{$table}` ({$fields}) values {$values}";
        
        $this->lastSql = $sql;
        
        return $this->client->query($sql);
    }
    
    public function add($table, $data) {
        $fields = array_keys($data);
        foreach ($fields as &$field) {
            $field = '`' . $field . '`';
        }
        
        $fields = implode(',', $fields);
        $values = [];
        foreach ($data as $v) {
            array_push($values, '"' . $v . '"');
        }
        
        $values = '(' . implode(',', $values) . ')';
        
        $sql = "insert into `{$table}` ({$fields}) values {$values}";
        
        $this->lastSql = $sql;
        
        return $this->client->query($sql);
    }
    
    public function find($table, $where, $field) {
        $where = $this->where($where);
        $sql = "select {$field} from `{$table}` where {$where}" . ' limit 1';
        $this->lastSql = $sql;
        $res = $this->client->query($sql);
        if(empty($res)){
            return [];
        }else{
            return $res[0];
        }
    }
    
    public function value($table, $where, $field) {
        $result = $this->find($table, $where, $field);
        if($result){
            return $result[$field];
        }else{
            return '';
        }
    }
    
    public function update($table, $where=[], $data=[]) {
        $update = '';
        foreach ($data as $k => $v) {
            $update .= "`{$k}` = '{$v}',";
        }
        
        $update = trim($update, ',');
        
        $where = $this->where($where);
        
        $sql = "update `{$table}` set {$update} where {$where}";
        
        $this->lastSql = $sql;
        
        return $this->client->query($sql);
    }
    
    public function where($where) {
        if(is_string($where)){
            return $where;
        }
        
        $_where = '1=1';
        foreach ($where as $k => $v) {
            $_where .= " and `{$k}` = '{$v}'";
        }
        
        return $_where;
    }
    
    
    public function begin() {
        return $this->client->begin();
    }
    
    public function rollback() {
        return $this->client->rollback();
    }
    
    public function close() {
        $this->client->close();
    }
    
    
}